package com.longsichao.lscframe.view;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface.OnClickListener;
import android.support.annotation.StringRes;
import android.view.View;

/**
 * @author SONG GUANYULONGSICHAO (leo.song[at]outlook[dot]com)
 * @version 1
 */
public final class LSCDialog {

    /**
     * Using a context for the AlertDialog it creates.
     *
     * @param context Current activity context.
     * @return This dialog object to allow for chaining of calls to set methods.
     */
    public static LSCDialog getDialog(Context context) {
        return new LSCDialog(context);
    }

    private Builder mBuilder = null;

    private LSCDialog(Context context) {
        mBuilder = new Builder(context, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
    }

    /**
     * Set the message to display.
     *
     * @param message The message to display.
     * @return This dialog object to allow for chaining of calls to set methods.
     */
    public LSCDialog setMessage(CharSequence message) {
        mBuilder.setMessage(message);
        return this;
    }

    /**
     * Set the message to display using the given resource id.
     *
     * @param messageId The message to display.
     * @return This dialog object to allow for chaining of calls to set methods.
     */
    public LSCDialog setMessage(@StringRes int messageId) {
        mBuilder.setMessage(messageId);
        return this;
    }

    /**
     * Set a listener to be invoked when the negative button of the dialog is pressed.
     *
     * @param text     The text to display in the negative button
     * @param listener The {@link OnClickListener DialogInterface.OnClickListener} to use.
     * @return This dialog object to allow for chaining of calls to set methods.
     */
    public LSCDialog setNegativeButton(CharSequence text, final OnClickListener listener) {
        mBuilder.setNegativeButton(text, listener);
        return this;
    }

    /**
     * Set a listener to be invoked when the negative button of the dialog is pressed.
     *
     * @param textId   The resource id of the text to display in the negative button.
     * @param listener The {@link OnClickListener DialogInterface.OnClickListener} to use.
     * @return This dialog object to allow for chaining of calls to set methods.
     */
    public LSCDialog setNegativeButton(@StringRes int textId, final OnClickListener listener) {
        mBuilder.setNegativeButton(textId, listener);
        return this;
    }

    /**
     * Set a listener to be invoked when the positive button of the dialog is pressed.
     *
     * @param text     The resource id of the text to display in the positive button.
     * @param listener The {@link OnClickListener DialogInterface.OnClickListener} to use.
     * @return This dialog object to allow for chaining of calls to set methods.
     */
    public LSCDialog setPositiveButton(CharSequence text, final OnClickListener listener) {
        mBuilder.setPositiveButton(text, listener);
        return this;
    }

    /**
     * Set a listener to be invoked when the positive button of the dialog is pressed.
     *
     * @param textId   The resource id of the text to display in the positive button.
     * @param listener The {@link OnClickListener DialogInterface.OnClickListener} to use.
     * @return This dialog object to allow for chaining of calls to set methods.
     */
    public LSCDialog setPositiveButton(@StringRes int textId, final OnClickListener listener) {
        mBuilder.setPositiveButton(textId, listener);
        return this;
    }

    /**
     * Set the title displayed in the dialog.
     *
     * @param title Dialog title string.
     * @return This dialog object to allow for chaining of calls to set methods.
     */
    public LSCDialog setTitle(CharSequence title) {
        mBuilder.setTitle(title);
        return this;
    }

    /**
     * Set the title using the given resource id.
     *
     * @param titleId Dialog title string.
     * @return This dialog object to allow for chaining of calls to set methods.
     */
    public LSCDialog setTitle(@StringRes int titleId) {
        mBuilder.setTitle(titleId);
        return this;
    }

    /**
     * Set a custom view to be the contents of the Dialog. If the supplied view is an instance
     * of a {@link android.widget.ListView} the light background will be used.
     *
     * @param view The view to use as the contents of the Dialog.
     * @return This Builder object to allow for chaining of calls to set methods
     */
    public LSCDialog setView(View view) {
        mBuilder.setView(view);
        return this;
    }

    public LSCDialog notCancelable() {
        mBuilder.setCancelable(false);
        return this;
    }

    public AlertDialog create() {
        return mBuilder.create();
    }

    /**
     * Creates a AlertDialog with the arguments supplied and show the dialog.
     */
    public void show() {
        mBuilder.show();
    }
}
